unit clsSubLocalCobranca;

interface

uses Dm, dbtables, biblio;

type
  TSubLocalCobranca = class(TObject)
  private
    Local_Codigo: string;
    SubL_Codigo: string;
  public
    SubL_Nome: string;
    SubL_Endereco: string;
    function Gravar(p_Local_Codigo, p_subl_codigo: string): Byte;
    function Recuperar(p_Local_Codigo, p_subl_codigo: string): Byte;
    function Excluir(p_Local_Codigo, p_subl_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_local_codigo, p_nome: string);
    function RecuperarSubLocal(p_Local_Codigo: string): string;
  end;

implementation

function TSubLocalCobranca.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM SubLocalCobranca where Local_Codigo=:k_Local_Codigo and Subl_codigo=:k_Subl_codigo');
  tq.parambyname('k_Local_Codigo').asstring := strzero(p_Local_Codigo, 3);
  tq.parambyname('k_Subl_codigo').asstring := strzero(p_subl_codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO SubLocalCobranca VALUES(' +
      ':k_Local_Codigo,' +
      ':k_Subl_codigo,' +
      ':k_Subl_nome,' +
      ':k_Subl_endereco)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE SubLocalCobranca SET Subl_nome=:k_Subl_nome,' +
      'Subl_endereco=:k_subl_endereco ' +
      'WHERE Local_Codigo=:k_Local_Codigo and Subl_Codigo=:k_subl_codigo');
  end;
  tq.parambyname('k_Local_Codigo').asstring := strzero(p_Local_Codigo, 3);
  tq.parambyname('k_subl_codigo').asstring := strzero(p_subl_codigo, 3);
  tq.parambyname('k_Subl_nome').asstring := SubL_nome;
  tq.parambyname('k_Subl_endereco').asstring := Subl_endereco;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TSubLocalCobranca.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM SubLocalCobranca where Local_Codigo=:k_local_codigo and SubL_Codigo=:k_Subl_Codigo');
  tq.Params[0].asstring := strzero(p_Local_Codigo, 3);
  tq.params[1].asstring := strzero(p_subl_codigo, 3);
  tq.open;
  Local_Codigo := p_Local_Codigo;
  subl_codigo := p_subl_codigo;
  if tq.eof then begin
    subl_nome := '';
    subl_endereco := '';
    Result := 0;
  end else begin
    Subl_nome := tq.fieldbyname('Subl_nome').asstring;
    Subl_endereco := tq.fieldbyname('Subl_endereco').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TSubLocalCobranca.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM SubLocalCobranca WHERE Local_Codigo=:k_Local_Codigo and Subl_codigo=:k_subl_codigo');
  tq.parambyname('k_Local_Codigo').asstring := strzero(p_Local_Codigo, 3);
  tq.parambyname('k_subl_codigo').asstring := strzero(p_subl_codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TSubLocalCobranca.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from SubLocalCobranca where Local_codigo=:k_local_codigo and subl_nome>=:k_subl_nome order by subl_nome');
  p_q.parambyname('k_local_codigo').asstring := strzero(p_local_codigo, 3);
  p_q.parambyname('k_subl_nome').asstring := p_nome;
end;

function TSubLocalCobranca.RecuperarSubLocal;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from SubLocalCobranca where Local_Codigo=:a');
  tq.Params[0].asstring := strzero(p_Local_Codigo, 3);
  tq.open;
  Result := tq.fieldbyname('Subl_Codigo').asstring;
  tq.close;
  tq.free;
end;

end.
